package com.briup.pai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.pai.common.constant.AuthConstant;
import com.briup.pai.common.enums.PermissionTypeEnum;
import com.briup.pai.convert.PermissionConvert;
import com.briup.pai.dao.PermissionMapper;
import com.briup.pai.entity.po.Permission;
import com.briup.pai.entity.vo.PageVO;
import com.briup.pai.entity.vo.PermissionPageVO;
import com.briup.pai.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@CacheConfig(cacheNames = AuthConstant.PERMISSION_CACHE_PREFIX)
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private PermissionConvert permissionConvert;

    @Override
    @Cacheable(key = "#pageNum+'_'+#pageSize")
    public PageVO<PermissionPageVO> getPermissionByPage(Long pageNum, Long pageSize) {
        IPage<Permission> page = new Page<>(pageNum,pageSize);
        // 主逻辑很简单：查出所有目录，根据目录查菜单，根据菜单查按钮，逐级封装
        // 查目录
        LambdaQueryWrapper<Permission> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Permission::getType, PermissionTypeEnum.CATALOGUE.getType())
                .orderByAsc(Permission::getSort);
        IPage<Permission> cataloguePage = this.page(page, lqw);
        // 构建返回值对象
        PageVO<PermissionPageVO> permissionPageVO = new PageVO<>();
        permissionPageVO.setTotal(cataloguePage.getTotal());
        // 准备遍历得到数据
        permissionPageVO.setData(permissionConvert.po2PermissionPageVOList(cataloguePage.getRecords()).stream().peek(
                catalogue -> {
                    // 查询目录下的菜单
                    lqw.clear();
                    lqw.eq(Permission::getParentId, catalogue.getId())
                            .eq(Permission::getType, PermissionTypeEnum.MENU.getType())
                            .orderByAsc(Permission::getSort);
                    List<PermissionPageVO> menus = permissionConvert.po2PermissionPageVOList(list(lqw)).stream().peek(
                            menu -> {
                                // 查询菜单下的按钮
                                lqw.clear();
                                lqw.eq(Permission::getParentId, menu.getId())
                                        .eq(Permission::getType, PermissionTypeEnum.BUTTON.getType())
                                        .orderByAsc(Permission::getSort);
                                menu.setChildren(permissionConvert.po2PermissionPageVOList(list(lqw)));
                            }
                    ).toList();
                    catalogue.setChildren(menus);
                }
        ).toList());
        return permissionPageVO;
    }
}